<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Globals {

    public static function sendsms($mobile, $content, $lastpw = false) {

        require(dirname(Yii::app()->BasePath) . "/sms/yunpian.php");

        // $mobileids = $mobile . Globals::random(6);
        // $rest = sendSMS($mobile, $content, $mobileids);
        // return $rest;

        // $mobile  = '18681981237';   //号码
        // $mobileids    = '186819812371234569080909';  //号码唯一编号
        // $content = '【竞彩王】 8832 验证码，10分钟有效，请勿泄露。';        //内容
        // //即时发送
        // $res = sendSMS($mobile,$content,$mobileids);
        // echo $res;
        //智能匹配模版接口发送样例
        $apikey = "ce775aca3f9d7684b5b86f4b63d5f756"; //请用自己的apikey代替
        // $mobile = "18681981237"; //请用自己的手机号代替
        // $text="【云片网】您的验证码是7465";
        // $text="【云片网】您的验证码是384322。如非本人操作，请忽略本短信";
        if ($lastpw) {
            $text = "【竞彩王】正在找回密码，您的验证码是".$content;
            return send_sms($apikey,$text,$mobile);
        }else{
            $text = "【竞彩王】感谢您注册竞彩王，您的验证码是".$content;
            return send_sms($apikey,$text,$mobile);
        }


        //模板接口样例（不推荐。需要测试请将注释去掉。)
        /* 以下代码块已被注释
            $apikey = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; //请用自己的apikey代替
            $mobile = "xxxxxxxxxxx"; //请用自己的手机号代替
            $tpl_id = 1; //对应默认模板 【#company#】您的验证码是#code#
            $tpl_value = "#company#=云片网&#code#=1234";
            echo tpl_send_sms($apikey,$tpl_id, $tpl_value, $mobile);
        */

    }

    public static function isMobile($agent)
    {
        return (strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS"));
    }

    public static function getIP(){
        if(getenv('HTTP_CLIENT_IP')) { 
            $onlineip = getenv('HTTP_CLIENT_IP');
        } elseif(getenv('HTTP_X_FORWARDED_FOR')) { 
            $onlineip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif(getenv('REMOTE_ADDR')) { 
            $onlineip = getenv('REMOTE_ADDR');
        } else { 
            $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
        }
        return $onlineip;

    }
    //生成密码  todo 改成4位
    public static function random($len) {
        $srcstr = "1234567890";
        mt_srand();
        $strs = "";
        $strlen = strlen($srcstr) - 1;
        for ($i = 0; $i < $len; $i++) {
            $strs .= $srcstr[mt_rand(0, $strlen)];
        }
        return strtoupper($strs);
    }

    public static function getPassword($a) {
        return md5($a . Yii::app()->params['private_key']);
    }

    public static function percent($a, $b) {
        $a = intval($a);
        $b = intval($b);
        $a = $a == 0 ? 1 : $a;
        $b = $b == 0 ? 1 : $b;

        if ($a < $b) {
            $tmp = $b;
            $b = $a;
            $a = $tmp;
        }
        return round(($a - $b) / $b, 2) . '%';
    }

 
    public static function diff($a, $b) {
        $a = intval($a);
        $b = intval($b);

        if ($a < $b) {
            $tmp = $b;
            $b = $a;
            $a = $tmp;
        }
        return round($a - $b) . '小时';
    }

     /**
     * 截取并缩放头像图片
     * @auth porterzhang
     */
    public static function resizeImage($image, $width = 75)
    {

        $file = $image;
        $filename = basename($image);

        $filetype = strtolower(substr($filename, stripos($filename, '.') + 1));
        $fileurl = substr($image, 0, strripos($image, '/') + 1);
        $newfilename = $fileurl . substr($filename, 0, stripos($filename, '.')) . '1.' .
            $filetype;

        switch ($filetype) {
            case 'jpg':
                $im = imagecreatefromjpeg($file); //从 JPEG 文件或 URL 新建一图像
                break;
            case 'gif':
                $im = imagecreatefromgif($file); //从 png 文件或 URL 新建一图像
                break;
            case 'png':
                $im = imagecreatefrompng($file); //从 png 文件或 URL 新建一图像
                break;
            case 'bmp':
                $im = imagecreatefromwbmp($file); //从 WBMP 文件或 URL 新建一图像
                break;
        }
        $pic_width = imagesx($im);
        $pic_height = imagesy($im);

        if ($pic_height > $pic_width) {
            $y = $pic_height / 2 - $pic_width / 2;
            $x = 0;
            $image_size = $pic_width;
        } else {
            $x = $pic_width / 2 - $pic_height / 2;
            $y = 0;
            $image_size = $pic_height;
        }
        if (function_exists("imagecopyresampled")) {
            $newim = @imagecreatetruecolor($width, $width);
            imagealphablending($newim, false);
            imagesavealpha($newim, true);
            //重采样拷贝部分图像并调整大小
            //imagecopyresampled (resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
            imagecopyresampled($newim, $im, 0, 0, $x, $y, $width, $width, $image_size, $image_size);
        }

        switch ($filetype) {
            case 'jpg':
                imagejpeg($newim, $newfilename);
                break;
            case 'gif':
                imagegif($newim, $newfilename);
                break;
            case 'png':
                imagepng($newim, $newfilename);
                break;
            case 'bmp':
                imagewbmp($newim, $newfilename);
                break;
        }
        imagedestroy($im);
        imagedestroy($newim);

        return str_replace(dirname(Yii::app()->basePath), "", $newfilename);
    }



}